<script setup lang='ts'>
  import { onMounted, reactive, ref, nextTick } from 'vue';
  import { newLassoCattleApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import Pagination from "@/components/Pagination/index.vue";
  const formLabelWidth = '120px'
  const dataForm = ref()
  const action = reactive({
    disabled:false,
    dialogFormVisible:false,
    total:0,
    list:[] as any[],
  })
  const listQuery = reactive({
    page:1,
    limit:20,
  })
  interface formQuert {
    data_num:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    data_num: [{ required: true, trigger: 'blur', message: '请输入礼物券数量' }],
  }
  const resetQuery = ()=>{
    listQuery.page = 1
    action.list = []
    getList()
  }
  const handleForm = ()=>{
    action.dialogFormVisible = true
  }
  const getList = async()=> {
    const loading = Toastloading()
    const res = await newLassoCattleApi.trackpoolList(listQuery)
    loading.close()
    const { data=[] } = res?.data || {}
    action.list = data
  }
  const handleSubmit = ()=>{
    dataForm.value.validate((valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      newLassoCattleApi.updateTrackpool(form).then(()=>{
        loading.close()
        ToastSuccess('发放成功')
        getList()
        closeDialog()
      }).catch(()=>{
        loading.close()
      })
    })
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="labelBox">
      <div class="labelItem">
        <el-button type="primary" @click="resetQuery">刷新</el-button>
        <el-button type="primary" @click="handleForm">创建</el-button>
      </div>
    </div>
    <el-table
      class="xm-TableCenter"
      :data="action.list"
      border>
      <el-table-column min-width="120" prop="id" label="记录ID" />
      <el-table-column min-width="120" prop="admin_id" label="管理员ID" />
      <el-table-column min-width="120" prop="data_num" label="增加/扣减（券数）" />
      <el-table-column min-width="120" prop="after_num" sortable label="变动后礼物池" />
      <el-table-column min-width="120" prop="created_at" sortable label="操作时间" />
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <el-dialog
      top="5vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="45%"
      title="参与池修改">
        <el-form ref="dataForm" :model="form" :rules="formRules">
          <el-form-item :label-width="formLabelWidth" label="礼物券数量" prop="data_num">
            <el-input
              v-model="form.data_num"
              placeholder="请输入礼物券数量"
            />
          </el-form-item>
        </el-form>
        <template v-if="!action.disabled" #footer>
          <span class="dialog-footer">
            <el-button @click="closeDialog">取 消</el-button>
            <el-button type="primary" @click="handleSubmit()">确 定</el-button>
          </span>
        </template>
      </el-dialog>
  </div>
</template>
